<template>
  <div class="container">
    <el-card>
      <!-- 顶部区域 -->
      <el-row>
        <el-col :span="24"> 试题录入</el-col>
      </el-row>
      <hr class="hr" />
      <!-- /顶部区域 -->

      <!-- 顶部搜索表单 -->
      <el-form :model="from" label-width="80px" ref="form" :rules="rules">
        <!-- 学科 -->
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="120px" label="学科：" prop="subjectID">
              <el-select
                style="width: 100%"
                v-model="from.subjectID"
                placeholder="请选择"
                @change="Subject"
              >
                <el-option
                  :label="item.subjectName"
                  :value="item.id"
                  v-for="(item, i) in subjects"
                  :key="i"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 目录 -->
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="120px" label="目录：" prop="catalogID">
              <el-select
                style="width: 100%"
                v-model="from.catalogID"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in directory"
                  :key="index"
                  :label="item.directoryName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <!-- <Mulu @catalogID="catalogNames" :prop="rules" :keyID="sunid" /> -->
          </el-col>
        </el-row>
        <!-- /目录 -->
        <!-- 企业 -->
        <el-row>
          <el-col :span="8">
            <Qiye @enterprise="Enterprise" :prop="rules" />
          </el-col>
        </el-row>
        <!-- /企业 -->
        <!-- 城市 -->
        <el-row>
          <el-col :span="8">
            <Chengshi @Provinces="provinces" :prop="rules" />
          </el-col>
        </el-row>
        <!-- /城市 -->
        <!-- 方向 -->
        <el-row>
          <el-col :span="8">
            <Fangxiang @Direction="direction" :prop="rules" />
          </el-col>
        </el-row>
        <!-- /方向 -->
        <!-- 题型 -->
        <el-row>
          <el-col>
            <el-form-item label-width="120px" label="题型：">
              <el-radio v-model="from.questionType" label="1">单选</el-radio>
              <el-radio
                v-model="from.questionType"
                label="2"
                @change="onqingKong"
                >多选</el-radio
              >
              <el-radio v-model="from.questionType" label="3">简答</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /题型 -->
        <!-- 难度 -->
        <!-- 难度 -->
        <el-row>
          <el-col>
            <el-form-item label-width="120px" label="难度：">
              <el-radio v-model="from.difficulty" label="1">简单</el-radio>
              <el-radio v-model="from.difficulty" label="2">一般</el-radio>
              <el-radio v-model="from.difficulty" label="3">困难</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /难度 -->
        <!-- 题干 -->
        <el-row>
          <el-col>
            <el-form-item label-width="120px" label="题干：" prop="question">
              <quill-editor
                class="quill-editor"
                ref="myTextEditor"
                v-model="from.question"
                style="height: 200px"
              ></quill-editor>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /题干 -->
        <!-- 选项 -->
        <!-- 单选 -->
        <template v-if="from.questionType === '1'">
          <span class="span">选项： </span>
          <div v-for="(item, index) in arr" :key="index">
            <el-row v-if="index < 4">
              <el-col>
                <el-form-item label-width="120px">
                  <el-row>
                    <el-col :span="1">
                      <el-radio :label="index" v-model="radio"
                        >{{ item.code }}：</el-radio
                      >
                    </el-col>
                    <el-col :span="5">
                      <el-input v-model="item.title"></el-input>
                    </el-col>
                    <el-col :span="10">
                      <el-upload
                        class="upload-demo"
                        :action="item.img"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        multiple
                        :limit="3"
                        :on-exceed="handleExceed"
                      >
                        <el-button size="small" type="primary"
                          >点击上传</el-button
                        >
                      </el-upload>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-row>
            <el-col>
              <el-button class="add_btn" type="danger" size="small" disabled
                >+增加选项与答案</el-button
              >
            </el-col>
          </el-row>
        </template>
        <!-- 、单选 -->
        <!-- 多选 -->
        <template v-if="from.questionType === '2'">
          <span class="span">选项： </span>
          <div v-for="(item, index) in arr" :key="index">
            <el-checkbox-group v-model="checkList">
              <el-row v-if="index < questionIndex">
                <el-col>
                  <el-form-item label-width="120px">
                    <el-row>
                      <el-col :span="1">
                        <el-checkbox :label="index"
                          >{{ item.code }}：</el-checkbox
                        >
                      </el-col>
                      <el-col :span="5">
                        <el-input v-model="item.title"></el-input>
                      </el-col>
                      <el-col :span="10">
                        <el-upload
                          class="upload-demo"
                          action="#"
                          multiple
                          :limit="3"
                        >
                          <el-button class="upload_btn">点击上传</el-button>
                        </el-upload>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-checkbox-group>
          </div>
          <el-row>
            <el-col>
              <el-button
                class="add_btn"
                type="danger"
                size="small"
                @click="addItem"
                >+增加选项与答案</el-button
              >
            </el-col>
          </el-row>
        </template>
        <!-- /多选 -->
        <!-- 解析视频 -->
        <el-row>
          <el-col :span="8">
            <el-form-item
              label-width="120px"
              label="解析视频："
              prop="videoURL"
            >
              <el-input v-model="from.videoURL"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /解析视频 -->
        <!-- 答案简析 -->
        <el-row>
          <el-col>
            <el-form-item label-width="120px" label="答案解析：" prop="answer">
              <quill-editor
                ref="myTextEditor"
                class="quill-editor"
                v-model="from.answer"
                style="height: 200px"
              ></quill-editor>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /答案简析 -->
        <!-- 题目备注： -->
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="120px" label="题目备注：">
              <el-input
                type="textarea"
                :rows="2"
                resize="none"
                placeholder="请输入内容"
                v-model="from.remarks"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 题目备注： -->
        <!-- 题目标签： -->
        <el-row>
          <el-col :span="8">
            <!-- <Tage @tage="tages" /> -->
            <el-form-item label-width="120px" label="题目标签：">
              <el-select
                v-model="from.tags"
                multiple
                allow-create
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  :label="item.tagName"
                  :value="item.tagName"
                  v-for="(item, i) in tagts"
                  :key="i"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- /题目标签： -->
        <el-form-item label-width="120px" label="">
          <el-button
            :type="$route.query.id ? 'success ' : 'primary'"
            @click="onClick($event, $route.query.id)"
            >{{ $route.query.id ? '确认修改' : '确认提交' }}</el-button
          >
        </el-form-item>
      </el-form>
      <!-- /顶部搜索表单 -->
    </el-card>
  </div>
</template>

<script>
import { list } from '@/api/hmmm/subjects.js'
import { list as dlist } from '@/api/hmmm/directorys.js'
import { list as tlist } from '@/api/hmmm/tags.js'
import { add, detail } from '@/api/hmmm/questions.js'
// import Mulu from '@/module-hmmm/pages/comment/mulu.vue'
import Qiye from '@/module-hmmm/pages/comment/qiye.vue'
import Chengshi from '@/module-hmmm/pages/comment/chengshi.vue'
import Fangxiang from '@/module-hmmm/pages/comment/fangxiang.vue'
// import Tage from '@/module-hmmm/pages/comment/tage.vue'
// import Xueke from '@/module-hmmm/pages/comment/xueke.vue'
// 富文本
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'Questions',
  data() {
    return {
      // 复选框列表
      checkList: [],
      // 单选
      radio: '',
      // 学科列表
      subjects: [],
      // 目录
      directory: [],
      // 标签
      tagts: [],
      //  表单数据
      from: {
        subjectID: '', // 学科id
        catalogID: '', // 目录id
        enterpriseID: '', // 企业id
        province: '', // 城市
        city: '', // 地方
        direction: '', // 方向
        questionType: '1', // 题型
        difficulty: '1', // 难度
        question: '', // 题干
        videoURL: '', // 简析视频
        answer: '', // 答案简析
        remarks: '', // 试题背入
        tags: [], // 题目标签
        options: []
      },
      // 存储选项的数组
      arr: [
        {
          code: 'A', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'B', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'C', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'D', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'E', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'F', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'G', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'H', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'I', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'J', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        },
        {
          code: 'K', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案 true/false
        }
      ],
      // 选项存储的index
      questionIndex: 4,
      // 校验的数据
      rules: {
        subjectID: [{ required: true, message: '请选择学科' }],
        catalogID: [{ required: true, message: '请选择目录' }],
        enterpriseID: [{ required: true, message: '请选择企业' }],
        province: [{ required: true, message: '请选择地区' }],
        direction: [{ required: true, message: '请选择方向' }],
        question: [{ required: true, message: '请输入题干' }],
        answer: [{ required: true, message: '请输入答案解析' }],
        videoURL: [{ required: true, message: '请输入解析视频地址' }]
      },
      // 定义学科的id
      xueke: ''
    }
  },
  created() {
    this.getsubject()
    this.getXiangqing()
  },
  methods: {
    // 学科
    async getsubject() {
      try {
        const { data } = await list()
        // console.log(data)
        this.subjects = data.items
      } catch (err) {
        console.log('错误')
      }
    },
    // 学科触发的事件
    async Subject(val) {
      // console.log(val)
      try {
        const { data } = await dlist({ subjectID: val })
        // console.log(data)
        this.directory = data.items
      } catch (err) {
        console.log('错误')
      }
      this.gettags()
    },
    // 标签的请求
    async gettags() {
      try {
        const { data } = await tlist({
          subjectID: this.from.subjectID,
          state: 1 // 1开启，0 屏蔽
        })
        console.log(data)
        this.tagts = data.items
      } catch (err) {}
    },
    // 企业选中的值
    Enterprise(val) {
      this.from.enterpriseID = val
    },
    // 地区选中的值
    provinces(val) {
      // console.log(val)
      this.from.province = val.value
      this.from.city = val.CitysValue
    },
    // 方向选中的值
    direction(val) {
      this.from.direction = val
    },
    // 点击多选添加选择项添加一个
    addItem() {
      this.questionIndex += 1
    },
    // 标签选中的值
    tages(val) {
      this.from.tags = val
    },
    onClick(e, id) {
      // console.log(e, id)
      // 判断用户是选中了难度是什么
      if (this.from.difficulty === '1') {
        this.from.options = this.arr
      } else if (this.from.difficulty === '2') {
        this.from.options = this.arr
      }
      // 校验数据
      this.$refs.form.validate(async (val) => {
        if (!val) {
          return false
        } else {
          try {
            await add(this.from)
            // 判断是从哪个地方过来的
            if (id) {
              this.$message.success('修改成功')
            } else {
              this.$message.success('提交成功')
            }
            // 跳转
            this.$router.push('list')
          } catch (err) {
            this.$message.error('请稍后再试')
          }
        }
      })
    },
    // 定义修改的函数
    async getXiangqing() {
      if (this.$route.query.id) {
        const { data } = await detail({
          id: this.$route.query.id
        })
        // console.log(data)
        this.from = data
      }
    },
    // 清空输入框的内容
    onqingKong() {
      this.arr[0].title = ''
      this.arr[1].title = ''
      this.arr[2].title = ''
      this.arr[3].title = ''
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      )
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    }
    // 学科触发的
  },
  components: {
    Qiye,
    Chengshi,
    Fangxiang,
    quillEditor
    // Tage
  },
  watch: {
    '$route.query.id'() {
      this.getXiangqing()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  .el-form-item__label {
    width: 200px !important;
  }
  .hr {
    margin-top: 20px;
  }
}
.quill-editor {
  margin-bottom: 100px;
}
.add_btn {
  margin: 10px 0px 20px;
  margin-left: 120px;
}
.span {
  margin: 20px 0 0 78px;
  display: block;
  font-weight: 700;
}
</style>
